<template>
  <el-card class="home-sales">
    <template #header>
      <div class="card-header">
        <el-tabs class="card-tabs">
          <el-tab-pane label="销售额"></el-tab-pane>
          <el-tab-pane label="访问量"></el-tab-pane>
        </el-tabs>
        <div class="card-date-wrap">
          <el-radio-group size="small" class="card-date-select" v-model="selectedDate">
            <el-radio-button label="day">今日</el-radio-button>
            <el-radio-button label="week">本周</el-radio-button>
            <el-radio-button label="month">本月</el-radio-button>
            <el-radio-button label="year">本年</el-radio-button>
          </el-radio-group>

          <!-- 
            v-model 收集数据
            value-format="YYYY-MM-DD" 指定收集日期数据的格式
           -->
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
            value-format="YYYY-MM-DD"
          />
        </div>
      </div>
    </template>

    <!-- 默认插槽 -->
    <el-row>
      <el-col :xs="24" :md="18">
        <SalesChart />
      </el-col>
      <el-col :xs="24" :md="6">
        <div class="rank-wrap">
          <div class="rank-title">门店销售额排名</div>

          <div v-for="(rank, index) in ranks" :key="rank.id" class="rank-item">
            <span>
              <span
                :class="{
                  'rank-index': true,
                  'rank-active': index <= 2,
                }"
                >{{ index + 1 }}</span
              >
              <span>{{ rank.shopName }}</span>
            </span>
            <span>￥{{ rank.sales }}</span>
          </div>
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "HomeSales",
};
</script>

<script lang="ts" setup>
import dayjs from "dayjs";
import { ref, watch } from "vue";
import SalesChart from "./SalesChart.vue";

const ranks = ref([
  { id: 1, shopName: "肯德基", sales: "323,234" },
  { id: 2, shopName: "肯德基1", sales: "323,111" },
  { id: 3, shopName: "肯德基2", sales: "323,222" },
  { id: 4, shopName: "肯德基3", sales: "323,333" },
  { id: 5, shopName: "肯德基4", sales: "323,444" },
  { id: 6, shopName: "肯德基5", sales: "323,555" },
  { id: 7, shopName: "肯德基6", sales: "323,666" },
]);

const dateRange = ref<any>("");

const selectedDate = ref("");

watch(selectedDate, (newVal) => {
  let start, end;
  const date = dayjs();

  switch (newVal) {
    case "day":
      start = date.format("YYYY-MM-DD");
      dateRange.value = [start, start];
      return;
    case "week":
      start = date.startOf("week").add(1, "day").format("YYYY-MM-DD");
      end = date.endOf("week").add(1, "day").format("YYYY-MM-DD");
      dateRange.value = [start, end];
      return;
    case "month":
      start = date.startOf("month").format("YYYY-MM-DD");
      end = date.endOf("month").format("YYYY-MM-DD");
      dateRange.value = [start, end];
      return;
    case "year":
      start = date.startOf("year").format("YYYY-MM-DD");
      end = date.endOf("year").format("YYYY-MM-DD");
      dateRange.value = [start, end];
      return;
  }
});
</script>

<style scoped>
.home-sales {
  margin-top: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  padding: 5px 20px 0;
  height: 50px;
}
.card-date-wrap {
  display: flex;
  align-items: center;
}

.card-date-select {
  margin-right: 10px;
}

.card-tabs {
  height: 100%;
}

.rank-wrap {
  padding: 0 10px;
}
.rank-title {
  font-size: 14px;
  margin-bottom: 20px;
}

.rank-item {
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.rank-index {
  display: inline-block;
  width: 20px;
  height: 20px;
  margin-right: 10px;
  text-align: center;
  line-height: 20px;
}

.rank-active {
  background-color: #000;
  color: #fff;
  border-radius: 50%;
}
</style>

<style>
.el-tabs__nav-wrap::after {
  display: none;
}
.el-tabs__header {
  margin: 0;
  height: 100%;
}
.el-card__header {
  padding: 0;
}
.el-tabs__item {
  height: 45px;
}
</style>
